package com.recursivechaos.examples.sqlwatcher.sql;
/**
 * Based on http://www.vogella.com/tutorials/MySQLJava/article.html
 * 
 * @author Andrew Bell
 */
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

public class MySQLAccess {
	private Connection connect = null;
	private Statement statement = null;
	private PreparedStatement preparedStatement = null;
	private ResultSet resultSet = null;

	  public void readDataBase() throws Exception {
		  try {
			  // This will load the MySQL driver, each DB has its own driver
		      Class.forName("com.mysql.jdbc.Driver"); //$NON-NLS-1$
		      // Setup the connection with the DB
		      connect = DriverManager
		          .getConnection(Messages.getString("MySQLAccess.0")); //$NON-NLS-1$
		      // PreparedStatements can use variables and are more efficient
		      preparedStatement = connect
		          .prepareStatement(Messages.getString("MySQLAccess.1")); //$NON-NLS-1$
		      resultSet = preparedStatement.executeQuery();
		      //writeResultSet(resultSet);
		      resultSet.next();
		      //System.out.println("Name: " + resultSet.getString("TRAIN_NAME"));
		  } catch (Exception e) {
			  // TODO Create unique exception
			  throw e;
		  } finally {
			  close();
		  }
	  }

	  // You need to close the resultSet
	  private void close() {
		  try {
			  if (resultSet != null) {
				  resultSet.close();
			  }
			  if (statement != null) {
				  statement.close();
			  }
			  if (connect != null) {
				  connect.close();
			  }
		  } catch (Exception e) {
			  //Throw unique exception?
		  }
	  }
}